package ldh.text;

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import ldh.text.util.GraphicUtil;
import org.kordamp.ikonli.fontawesome.FontAwesome;
import org.kordamp.ikonli.material.Material;

public class IconDemo extends Application {


    private ObservableList<Node> observableList = FXCollections.observableArrayList();
    private FilteredList<Node> filteredList = new FilteredList(observableList, data->true);

    @Override
    public void start(Stage primaryStage) throws Exception {
        FlowPane flowPane = new FlowPane();
        Bindings.bindContent(flowPane.getChildren(), filteredList);
//        flowPane.getChildren().addAll(filteredList);

        VBox vBox = new VBox(10);
        HBox hBox = createSearchPane();
        flowPane.setVgap(10);
        flowPane.setHgap(10);

        for(FontAwesome fontAwesome : FontAwesome.values()) {
            Label label = new Label();
            label.setUserData(fontAwesome.getDescription());
            label.setGraphic(GraphicUtil.createIcon(fontAwesome, 24, Color.BLUEVIOLET));
            label.setOnMouseClicked(e->System.out.println(fontAwesome.getDescription()));
//            flowPane.getChildren().add(label);
            observableList.add(label);
        }

        for(Material material : Material.values()) {
            Label label = new Label();
            label.setUserData(material.getDescription());
            label.setGraphic(GraphicUtil.createIcon(material, 24, Color.BLUEVIOLET));
            label.setOnMouseClicked(e->System.out.println(material.getDescription()));
//            flowPane.getChildren().add(label);
            observableList.add(label);
        }

        ScrollPane scrollPane = new ScrollPane();
        scrollPane.setContent(flowPane);
        scrollPane.setFitToHeight(true);
        scrollPane.setFitToWidth(true);

        vBox.getChildren().addAll(hBox, scrollPane);
        VBox.setVgrow(scrollPane, Priority.ALWAYS);
        Scene scene = new Scene(vBox, 600, 500);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private HBox createSearchPane() {
        HBox hBox = new HBox(10);
        Label label = new Label("查询");
        TextField textField = new TextField();
        Button b = new Button("查询");
        b.setOnAction(e->search(textField));
        textField.setOnAction(e->search(textField));
        hBox.getChildren().addAll(label, textField, b);
        hBox.setAlignment(Pos.CENTER_LEFT);
        return hBox;
    }

    private void search(TextField textField) {
        String t = textField.getText();
        if (t.trim().equals("")) return;
        filteredList.setPredicate(l->{
            Label label1 = (Label) l;
            String text = (String) label1.getUserData();
            return text.contains(t);
        });
    }
}
